package co.fitstart.admin.entity.content;

import java.util.Date;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import co.fitstart.entity.content.Status;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

/**
 * 
 * @author downpour
 */
public class ActivitySearch implements QueryBuilder {
    
    private String name;
    
    private Status status;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginDate;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
    
    private Order order;
    
    private Filter filter;
    
    public ActivitySearch() {
        order = new Order("serial", "DESC")
                    .add("createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {
            filter = new Filter().addLike("name", name)
                                 .addEquals("status", status)
                                 .addDateTimeBetween("beginDate", "endDate", beginDate, endDate)
                                 .addEquals("disabled", false);
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM Activity " + getPreparedFilter(false).getWhereSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the status
     */
    public Status getStatus() {
        return status;
    }
    
    /**
     * @return the beginDate
     */
    public Date getBeginDate() {
        return beginDate;
    }

    /**
     * @return the endDate
     */
    public Date getEndDate() {
        return endDate;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(Status status) {
        this.status = status;
    }
    
    /**
     * @param beginDate the beginDate to set
     */
    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }
    
    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }
    
}
